<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/12/14
 */

namespace App\Http\Controllers;

use App\Filters\RoleFilter;
use App\Http\Requests\RoleRequest;
use App\Http\Resources\RoleCollection;
use App\Http\Resources\RoleResource;
use App\Models\Role;
use App\Services\RoleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 角色管理模块
 * Class RoleController
 * @package App\Http\Controllers
 */
class RoleController extends Controller
{

    /**
     * 列表
     * @param RoleFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(RoleFilter $filter)
    {
        $paginate = Role::filter($filter)->paginate($this->pageSize());
        $result = RoleCollection::make($paginate)->except(['category_ids', 'menu_permission_ids'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param RoleRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(RoleRequest $request)
    {
        $data = $request->validate('store');
        DB::beginTransaction();
        try {
            RoleService::updateOrCreate(null, $data);
            DB::commit();
            return $this->successStored();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorStored();
        }
    }

    /**
     * 查看
     * @param Role $role
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Role $role)
    {
        $roleCollection = RoleResource::make($role)->except(['created_at', 'updated_at', 'sort'])->toArray(null);
        return $this->success('', $roleCollection);
    }

    /**
     * 更新
     * @param RoleRequest $request
     * @param Role $role
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(RoleRequest $request, Role $role)
    {
        $data = $request->validate('update');
        DB::beginTransaction();
        try {
            RoleService::updateOrCreate($role, $data);
            DB::commit();
            return $this->successUpdated();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorUpdated();
        }
    }

    /**
     * 删除
     * @param $id // 1或者1,2
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            Role::destroy($between);
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param RoleRequest $request
     * @param Role $role
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(RoleRequest $request, Role $role)
    {
        $data = $request->validate('updateStatus');
        if (!$role->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param RoleRequest $request
     * @param Role $role
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(RoleRequest $request, Role $role)
    {
        $data = $request->validate('updateSort');
        if (!$role->batchUpdate($data['sort'])) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 场景：管理员模块中用户角色配置
     * @return \Illuminate\Http\JsonResponse
     */
    public function userRoleConfig()
    {
        return $this->success('', ['items' => RoleService::getAllList()]);
    }


}
